package com.gxpt.app.view;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gxpt.app.BaseFragmentActivity;
import com.gxpt.app.R;
import com.gxpt.app.fragment.Fragment_authen_gr;
import com.gxpt.app.fragment.Fragment_authen_qy;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dufeng on 2017/8/4.
 */

@ContentView(R.layout.activity_center_authen)
public class Activity_center_authen extends BaseFragmentActivity {

    Context context;
    Fragment                          fragment_authen_gr;
    Fragment                          fragment_authen_qy;
    Fragment                          currentFragment;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private static final  String[] FRAGMENT_TAG = {"gr","qy"};
    List<Fragment> fragments = new ArrayList<Fragment>();

    @ViewInject(R.id.toback)
    Button toback;

    @ViewInject(R.id.txt_title)
    TextView txt_title;

    @ViewInject(R.id.mainListLayout)
    LinearLayout mainListLayout;

    @ViewInject(R.id.submenu0)
    LinearLayout submenu0;

    @ViewInject(R.id.submenu1)
    LinearLayout submenu1;

    @ViewInject(R.id.line0)
    LinearLayout line0;

    @ViewInject(R.id.line1)
    LinearLayout line1;

    int index = 0 ;//menu
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        context = this;
        /**
         * 拿到事务管理器并开启事务
         */
        manager = getSupportFragmentManager();
        initView();
    }


    public void initView() {
        txt_title.setText(getResources().getString(R.string.txt_my_posdcar));
        fragment_authen_gr   = new Fragment_authen_gr();
        fragments.add(0,fragment_authen_gr);
        fragment_authen_qy   = new Fragment_authen_qy();
        fragments.add(0,fragment_authen_qy);
        setDefaultFragment();
    }


    private void setDefaultFragment() {
        transaction = manager.beginTransaction();
        transaction.add(R.id.fragment_content, fragment_authen_gr,FRAGMENT_TAG[index]).commit();
        currentFragment = fragment_authen_gr;
    }

    /**
     * 修改显示的内容 不会重新加载 *
     */
    public void switchContent(Fragment to) {
        if (!currentFragment.getTag().equals(to.getTag())) {

            transaction = manager.beginTransaction();
            if (!to.isAdded()) { // 先判断是否被add过
                transaction.hide(currentFragment).add(R.id.fragment_content, to,FRAGMENT_TAG[index]).commit(); // 隐藏当前的fragment，add下一个到Activity中
            } else {
                transaction.hide(currentFragment).show(to).commit(); // 隐藏当前的fragment，显示下一个

            }
            currentFragment = to;
        }
    }

    private void changeMenuView(){
        switch (index){
            case 0:
                line0.setBackgroundColor(getResources().getColor(R.color.red));
                line1.setBackgroundColor(getResources().getColor(R.color.white));
                break;
            case 1:
                line0.setBackgroundColor(getResources().getColor(R.color.white));
                line1.setBackgroundColor(getResources().getColor(R.color.red));
                break;
        }
    }

    @Event(value={R.id.toback,R.id.submenu0,R.id.submenu1})
    private void getEvent(View view){
        switch(view.getId()){
            case R.id.toback:
                finish();
                break;
            case R.id.submenu0:
                if(index != 0){
                    index = 0 ;
                    changeMenuView();
                    switchContent(fragment_authen_gr);
                }
                break;
            case R.id.submenu1:
                if(index != 1){
                    index = 1 ;
                    changeMenuView();
                    switchContent(fragment_authen_qy);
                }
                break;
        }
    }




}
